package cn.teawooo.tree.mvnpro;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.teawooo.tree.mvnpro.project.MvnModule;
import cn.teawooo.tree.mvnpro.project.PackagingType;
import cn.teawooo.tree.node.AbstractNode;
import cn.teawooo.tree.node.Node;

/**
 * 功能描述：
 * 
 * @author teawooo <long1795@gmail.com>
 * @time 2013年8月26日 下午2:56:58
 */
public class MvnNode extends AbstractNode<MvnModule> {

    /**
     * @param name
     * @param data
     */
    public MvnNode(String name, MvnModule data) {
        super(name, data);
    }

    public MvnNode(MvnModule data) {
        super(data.getName() == null ? data.getArtifactId() : data.getName(), data);
    }

    @Override
    public <C extends Node<MvnModule>> C grownAndReturnChild(C child) {
        super.grownAndReturnChild(child);
        data.setPackaging(PackagingType.pom);
        return child;
    }

    @Override
    protected <P extends Node<MvnModule>> void grownImpl(P child) {
        super.grownImpl(child);
        data.setPackaging(PackagingType.pom);
    }

    @Override
    protected <P extends Node<MvnModule>> void dependImpl(P parent) {
        super.dependImpl(parent);
        this.parent.getData().setPackaging(PackagingType.pom);
    }

    public MvnNode fastGrown(String... childNames) {
        for (String childName : childNames) {
            fastGrown(childName, PackagingType.jar);
        }
        return this;
    }

    public MvnNode fastGrown(String childName) {
        int i = childName.indexOf('{');
        if (i >= 0) {
            String name = childName.substring(0, i);
            String type = childName.substring(i + 1);
            return fastGrown(name, PackagingType.valueOf(type));
        } else {
            return fastGrown(childName, PackagingType.jar);
        }
    }

    public MvnNode fastGrown(String childName, PackagingType pt) {
        MvnModule mm = new MvnModule();
        mm.setName(childName);
        mm.setArtifactId(getData().getArtifactId() + "-" + childName);//---
        mm.setGroupId(data.getGroupId() + "." + data.getName());//---
        mm.setPackaging(pt);
        mm.setVersion(data.getVersion());
        data.setPackaging(PackagingType.pom);
        return grown(new MvnNode(mm));
    }

    public MvnNode fastChainGrown(String... childNames) {
        return fastChainGrown(PackagingType.jar, childNames);
    }

    public MvnNode fastChainGrown(PackagingType pt, String... childNames) {
        List<String> names = new ArrayList<String>();
        for (String childName : childNames) {
            names.add(childName);
        }
        return fastChainGrown(pt, names);
    }

    public MvnNode fastChainGrown(PackagingType pt, List<String> childNames) {
        if (childNames == null || childNames.isEmpty()) {
            return null;
        }
        String childName = childNames.remove(0);

        MvnModule mm = new MvnModule();

        int i = childName.indexOf('{');
        if (i >= 0) {
            String type = childName.substring(i + 1);
            mm.setPackaging(PackagingType.valueOf(type));
            childName = childName.substring(0, i);
        } else {
            mm.setPackaging(pt);
        }

        mm.setName(childName);
        mm.setArtifactId(childName);
        mm.setGroupId(data.getGroupId() + "." + data.getArtifactId());
        mm.setVersion(data.getVersion());
        data.setPackaging(PackagingType.pom);

        MvnNode child = new MvnNode(mm);
        child.mount(this);

        if (childNames == null || childNames.isEmpty()) {
            return child;
        }

        return child.fastChainGrown(pt, childNames);
    }

    public String getNodePath(String rootPath) {
        List<MvnNode> pc = getParentChain();
        Collections.reverse(pc);
        StringBuffer path = new StringBuffer(rootPath);
        if (!rootPath.endsWith(File.separator)) {
            path.append(File.separator);
        }
        for (Node<MvnModule> n : pc) {
            path.append(n.getData().getArtifactId());
            path.append(File.separator);
        }
        return path.toString();
    }
}
